﻿using MediatR;
using ThreeTeams.API.Application.Command.ShelvesCommand;
using ThreeTeams.Domain;
using ThreeTeams.ErrorCode;
using ThreeTeams.Infrastructure;

namespace ThreeTeams.API.Application.CommandHandler.ShelvesCommandHandler
{
    /// <summary>
    /// 货架删除命令处理器
    /// </summary>
    public class DeleteShelvesCommandHandler : IRequestHandler<DeleteShelvesCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<Shelves> baseRepository;
        public DeleteShelvesCommandHandler(IBaseRepository<Shelves> baseRepository)
        {
            this.baseRepository = baseRepository;
        }
        public async Task<ApiResult<int>> Handle(DeleteShelvesCommand request, CancellationToken cancellationToken)
        {
            var res = new ApiResult<int>();
            var shelves = baseRepository.GetAll().FirstOrDefault(x => x.Id == request.Id);
            if (shelves == null)
            {
                res.Code = ApiEnum.Fail;
                res.Message = "未找到数据";
                return res;
            }
            await baseRepository.Deleted(shelves);
            res.Code = ApiEnum.Ok;
            res.Message = "删除成功";
            res.Data = shelves.Id;
            return res;
        }
    }
}
